package com.mouse.hxsj.view;

import com.mouse.damo.micro.Com;
import com.mouse.hxsj.constants.ComPool;
import com.mouse.hxsj.entity.common.Hwnd;
import com.mouse.hxsj.service.FamlilyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.*;
import java.util.List;

@Component
@Slf4j
public class FamilyFrm extends JInternalFrame {
    JComboBox comboBox;
    JComboBox comboBox_1;
    JButton button_4;
    JComboBox comboBox_4;
    FamlilyService familyService = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FamilyFrm frame = new FamilyFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public FamilyFrm() {
        setTitle("家族领地");
        setClosable(true);
        setIconifiable(true);
        setBounds(100, 100, 450, 300);

        JLabel label = new JLabel("家族任务先送花");
        label.setForeground(Color.RED);

        JLabel lblf = new JLabel("家族传送符放F4");

        JLabel lblf_1 = new JLabel("领地传送符放F5");

        JLabel lblb = new JLabel("提交物品请手动,再按B");

        JLabel label_1 = new JLabel("类型:");

        comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[]{"家族", "领地"}));
        JLabel lblNewLabel = new JLabel("次数:");

        button_4 = new JButton("启动");
        button_4.addActionListener(e -> startLoad());
        comboBox_1 = new JComboBox();
        comboBox_1.setModel(new DefaultComboBoxModel(new String[]{"10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "0"}));

        JLabel label_2 = new JLabel("句柄:");

        comboBox_4 = new JComboBox();
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(149)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
                                                        .addComponent(lblf_1, GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                                                        .addComponent(lblf, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(lblb, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addContainerGap()
                                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                                        .addComponent(button_4)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(label_2)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(comboBox_4, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(26)
                                                                .addComponent(label_1)))
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(lblNewLabel)
                                                .addGap(4)
                                                .addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)))
                                .addGap(60))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(39)
                                .addComponent(label)
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addComponent(lblf)
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addComponent(lblf_1)
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addComponent(lblb)
                                .addGap(27)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_2)
                                        .addComponent(comboBox_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_1)
                                        .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addComponent(button_4)
                                .addContainerGap(52, Short.MAX_VALUE))
        );
        getContentPane().setLayout(groupLayout);
        familyService = new FamlilyService();
    }

    private void initHwnd() {
        log.info("检测绑定句柄");
        java.util.List<Com> comList = ComPool.getComList();
        List<String> accountList = ComPool.getAccountList();
        comboBox_4.removeAllItems();
        for (int i = 0; i < comList.size(); i++) {
            Hwnd hwnd = new Hwnd();
            hwnd.setHwnd(comList.get(i).getHwnd());
            hwnd.setAccount(accountList.get(i));
            comboBox_4.addItem(hwnd);
        }
    }

    boolean isFirst = true;

    private void startLoad() {
        int selectedIndex = comboBox_4.getSelectedIndex();
        if (selectedIndex == -1) return;
        List<Com> comList = ComPool.getComList();
        Com com = comList.get(selectedIndex);
        boolean lingdi = comboBox.getSelectedItem().
                toString().equals("领地") ? true : false;
        if (!isFirst) {
            if (button_4.getText().equals("关闭")) {
                button_4.setText("开启");
                familyService.setOpen(false);
                familyService.setCom(com);
                familyService.setLingdi(lingdi);

            } else {
                button_4.setText("关闭");
                familyService.setOpen(true);
                familyService.setCom(com);
                familyService.setLingdi(lingdi);
            }
            return;
        }
        com.clearDict(7);
        com.setDict(7, "家族领地.txt");
        familyService.setOpen(true);
        familyService.setCom(com);
        familyService.setLingdi(lingdi);
        isFirst=false;
        button_4.setText("关闭");
        new Thread(() -> {
            familyService.start1();
        }).start();
    }

    public void initFrm() {
        initHwnd();
    }
}
